// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 检查用户是否已登录
    checkLoginStatus();
    
    // 获取表单和元素
    const loginForm = document.getElementById('loginForm');
    const loginMessage = document.getElementById('loginMessage');
    const registerForm = document.getElementById('registerForm');
    const registerMessage = document.getElementById('registerMessage');
    const loginArea = document.getElementById('loginArea');
    const registerArea = document.getElementById('registerArea');
    const showRegisterLink = document.getElementById('showRegisterLink');
    const showLoginLink = document.getElementById('showLoginLink');
    
    // 切换到注册表单
    if (showRegisterLink && loginArea && registerArea) {
        showRegisterLink.addEventListener('click', function(e) {
            e.preventDefault();
            loginArea.style.display = 'none';
            registerArea.style.display = 'block';
        });
    }
    
    // 切换到登录表单
    if (showLoginLink && loginArea && registerArea) {
        showLoginLink.addEventListener('click', function(e) {
            e.preventDefault();
            registerArea.style.display = 'none';
            loginArea.style.display = 'block';
        });
    }
    
    // 为登录表单添加提交事件监听器
    if (loginForm && loginMessage) {
        loginForm.addEventListener('submit', function(e) {
            // 阻止表单的默认提交行为
            e.preventDefault();
            
            // 获取表单数据
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            // 简单的客户端验证
            if (!username || !password) {
                showLoginMessage('用户名和密码不能为空', 'error');
                return;
            }
            
            // 从localStorage获取用户数据
            const users = JSON.parse(localStorage.getItem('users')) || [];
            
            // 查找匹配的用户
            const user = users.find(u => u.username === username && u.password === password);
            
            if (user) {
                // 登录成功，保存登录状态
                localStorage.setItem('currentUser', JSON.stringify(user));
                showLoginMessage('登录成功！正在跳转到主页面...', 'success');
                
                // 2秒后跳转到主页面
                setTimeout(() => {
                    window.location.href = 'main.html';
                }, 2000);
            } else {
                showLoginMessage('用户名或密码错误', 'error');
            }
            
            // 重置表单
            loginForm.reset();
        });
    }
    
    // 为注册表单添加提交事件监听器
    if (registerForm && registerMessage) {
        registerForm.addEventListener('submit', function(e) {
            // 阻止表单的默认提交行为
            e.preventDefault();
            
            // 获取表单数据
            const username = document.getElementById('regUsername').value;
            const password = document.getElementById('regPassword').value;
            const confirmPassword = document.getElementById('regConfirmPassword').value;
            
            // 简单的客户端验证
            if (!username || !password || !confirmPassword) {
                showRegisterMessage('所有字段都不能为空', 'error');
                return;
            }
            
            if (password !== confirmPassword) {
                showRegisterMessage('两次输入的密码不一致', 'error');
                return;
            }
            
            if (password.length < 6) {
                showRegisterMessage('密码长度至少为6位', 'error');
                return;
            }
            
            // 从localStorage获取用户数据
            const users = JSON.parse(localStorage.getItem('users')) || [];
            
            // 检查用户名是否已存在
            if (users.some(u => u.username === username)) {
                showRegisterMessage('用户名已存在', 'error');
                return;
            }
            
            // 添加新用户
            users.push({ username, password });
            
            // 保存到localStorage
            localStorage.setItem('users', JSON.stringify(users));
            
            showRegisterMessage('注册成功！请登录', 'success');
            
            // 重置表单并切换到登录页面
            registerForm.reset();
            setTimeout(() => {
                registerArea.style.display = 'none';
                loginArea.style.display = 'block';
            }, 2000);
        });
    }
    
    // 显示登录消息的函数
    function showLoginMessage(message, type) {
        loginMessage.textContent = message;
        loginMessage.className = type;
        loginMessage.style.display = 'block';
        
        // 3秒后自动隐藏消息
        setTimeout(function() {
            loginMessage.style.display = 'none';
        }, 3000);
    }
    
    // 显示注册消息的函数
    function showRegisterMessage(message, type) {
        registerMessage.textContent = message;
        registerMessage.className = type;
        registerMessage.style.display = 'block';
        
        // 3秒后自动隐藏消息
        setTimeout(function() {
            registerMessage.style.display = 'none';
        }, 3000);
    }
    
    // 检查用户登录状态
    function checkLoginStatus() {
        const currentUser = localStorage.getItem('currentUser');
        if (currentUser && window.location.pathname.endsWith('index.html')) {
            window.location.href = 'main.html';
        }
    }
    
    // 添加平滑滚动效果
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            
            document.querySelector(this.getAttribute('href')).scrollIntoView({
                behavior: 'smooth'
            });
        });
    });
});